<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='物料分类'>
                <selectMaterielType @selectClick="selectClick" :value="queryParam.materielTypeId"></selectMaterielType>
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='物料商品名称'>
              <a-input placeholder='请输入物料商品名称' v-model='queryParam.materielGoodsName'></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' v-has="'xhdjMaterielGoods:add'" type='primary' icon='plus'>新增</a-button>
<!--      <a-button type='primary' icon='download' @click="handleExportXls('物料商品')">导出</a-button>-->
<!--      <a-upload name='file' :showUploadList='false' :multiple='false' :headers='tokenHeader' :action='importExcelUrl'-->
<!--                @change='handleImportExcel'>-->
<!--        <a-button type='primary' icon='import'>导入</a-button>-->
<!--      </a-upload>-->
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay' v-has="'xhdjMaterielGoods:delete'">
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        class='j-table-force-nowrap'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        @change='handleTableChange'>

<!--        <template slot="goodsImg" slot-scope="text, record">-->
<!--          <div class="anty-img-wrap">-->
<!--            <a-avatar shape="square" :src="getAvatarView(record.goodsImg)" />-->
<!--          </div>-->
<!--        </template>-->

        <template slot="goodsImg" slot-scope="text, record">
          <div class="anty-img-wrap">
            <j-image-upload v-if="record.goodsImg" text="上传" disabled v-model="record.goodsImg"></j-image-upload>
          </div>
        </template>
        <template slot="status" slot-scope="text, record">
          <a-switch default-checked :checked="record.status" @change="statusChange(record)" />
        </template>
<!--        <template v-slot:goodsImg="{ record }">-->
<!--          <div class="anty-img-wrap">-->
<!--            <a-avatar shape="square" :src="getAvatarView(record.goodsImg)" />-->
<!--          </div>-->
<!--        </template>-->

        <span slot='action' slot-scope='text, record'>
          <a v-has="'xhdjMaterielGoods:update'" @click='handleEdit(record)'>编辑</a>
          <a-divider type='vertical' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay' v-has="'xhdjMaterielGoods:delete'">
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <xhdjMaterielGoods-modal ref='modalForm' @ok='modalFormOk'></xhdjMaterielGoods-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import XhdjMaterielGoodsModal from './modules/XhdjMaterielGoodsModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, getFileAccessHttpUrl, httpAction } from '@api/manage'
import selectMaterielType from './modules/SelectMaterielType.vue'

export default {
  name: 'XhdjMaterielGoodsList',
  mixins: [JeecgListMixin],
  components: {
    XhdjMaterielGoodsModal,selectMaterielType
  },
  data() {
    return {
      materielTypedataForm:[],
      description: '物料商品管理页面',
      // 表头
      columns: [
        {
          width: 180,
          title: '物料分类名称',
          align: 'center',
          dataIndex: 'materielTypeName'
        },
        {
          width: 180,
          title: '物料商品名称',
          align: 'center',
          dataIndex: 'materielGoodsName'
        },
        {
          width: 130,
          title: '商品封面',
          align: 'center',
          dataIndex: 'goodsImg',
          scopedSlots: { customRender: 'goodsImg' }
        },
        {
          width: 160,
          title: '商品价格',
          align: 'center',
          dataIndex: 'price',
          customRender: (text, record) => {
            const unitTypes = ['个', '套', '瓶', '件', '条'];
            const unitType = unitTypes[record.unitType];
            const priceWithUnit = `${record.price}元/${unitType}`;
            return priceWithUnit;
          },
        },
        {
          title: '是否上架',
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/material/xhdjMaterielGoods/list',
        delete: '/material/xhdjMaterielGoods/delete',
        deleteBatch: '/material/xhdjMaterielGoods/deleteBatch',
        exportXlsUrl: 'material/xhdjMaterielGoods/exportXls',
        importExcelUrl: 'material/xhdjMaterielGoods/importExcel',
        allList: '/material/xhdjMaterielType/allList',
        edit: "/material/xhdjMaterielGoods/edit",

      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },

  methods: {
    selectClick (value) {
      this.queryParam.materielTypeId = value
      this.searchQuery()
    },
    // 改变状态
    statusChange (record) {
      let a = {
        id: record.id,
        status: record.status ? 0 : 1
      }
      httpAction(this.url.edit, a, 'post').then(res => {
        if (res.success) {
          this.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    handleSelectClick () {
      console.log("23123")
      getAction(this.url.allList).then(res => {
        if (res.code === 200) {
          this.materielTypedataForm = res.result
        }
      })
    },
    handleSelectChange(value) {
      // 下拉选择框值发生变化时的处理逻辑
      console.log('下拉选择框值发生变化');
      console.log('当前选择的值:', value);
      // 将选中的值赋给 queryParam.materielTypeId
      this.queryParam.materielTypeId = value;
      // 在这里可以根据选择的值进行相关操作
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.anty-img-wrap /deep/ .ant-upload-picture-card-wrapper {
  display: block !important;
}
</style>